import { createApp } from 'vue'
import axios from 'axios';
import router from './routes';
import store from './store';
import App from './App.vue';
import './assets/styles/base.less';
import { formatDate } from './utils';

async function main() {

    axios.defaults.withCredentials = true;

    // 获取登录用户信息，防止浏览器刷新后需重新登录
    await store.dispatch('loginUser/getLoginUser');

    router.beforeEach((to, from) => {
        // 前往需要登录的页面但未登录时，跳转到登录页面
        if (to.meta.loginRequired && !store.getters['loginUser/isLogged']) {
            return { name: 'Login' };
        }
    });

    const app = createApp(App);

    app.config.globalProperties.formatDate = formatDate;

    app.use(router).use(store).mount('#app');
}

main();